import { checkUsername } from '/@/api/system/admin';
import { getRoleListPage, getRoleSelectList } from '/@/api/system/role';
import { BasicColumn, FormSchema } from '/@/components/Table';

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'true_name',
    width: 120,
  },
  {
    title: '电话',
    dataIndex: 'phone',
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 180,
  },
  {
    title: '角色',
    dataIndex: 'roles_name',
    width: 200,
  },
  {
    title: '最后一次登录时间',
    dataIndex: 'last_time',
    width: 180,
    // format: 'date|YYYY-MM-DD HH:mm:ss',
  },
  {
    title: '最后一次登录ip',
    dataIndex: 'last_ip',
    width: 180,
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '搜索',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: {
      placeholder: '请输入姓名或者账号',
    },
  },
  {
    field: 'ture_name',
    label: '姓名',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const usernameFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
      {
        validator(_, value) {
          return new Promise((resolve, reject) => {
            checkUsername(value)
              .then(() => resolve())
              .catch((err) => {
                reject(err.message || '验证失败');
              });
          });
        },
      },
    ],
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: false,
  },
  {
    label: '角色',
    field: 'roles',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleListPage,
      labelField: 'role_name',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'department',
    label: '所属部门',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'deptName',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    field: 'true_name',
    label: '姓名',
    component: 'Input',
    required: true,
  },

  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
  },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'ID',
    component: 'Input',
    show: false,
  },
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请输入密码',
      },
      {
        min: 4,
        max: 12,
        message: '请设置用户名长度4-12位',
      },
    ],
  },
  {
    field: 'true_name',
    label: '姓名',
    component: 'Input',
    required: true,
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    rules: [
      {
        required: true,
        message: '请输入密码',
      },
      {
        min: 6,
        max: 12,
        message: '请设置密码长度6-12位',
      },
    ],
  },
  {
    field: 'conf_password',
    label: '确认密码',
    component: 'InputPassword',
    dynamicRules: ({ values }) => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('密码不能为空');
            }
            if (value !== values.password) {
              return Promise.reject('两次输入的密码不一致!');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'phone',
    label: '手机号码',
    component: 'Input',
    rules: [
      {
        required: true,
        validator: (_, value) => {
          const phone = /^1[3456789]\d{9}$/;
          if (!value) {
            return Promise.reject('手机号不可为空');
          }
          if (phone.test(value)) {
            return Promise.resolve();
          } else {
            return Promise.reject('电话号码格式不正确!');
          }
        },
      },
    ],
  },

  // {
  //   field: 'roles',
  //   label: '角色',
  //   component: 'ApiSelect',
  //   required: true,
  //   componentProps: {
  //     api: getRoleListPage,
  //     resultField: 'list',
  //     labelField: 'role_name',
  //     valueField: 'id',
  //   },
  // },

  // TODO: 角色多选
  {
    field: 'roles',
    label: '角色',
    component: 'Select',
    required: true,
  },

  // {
  //   field: 'roles',
  //   label: '角色2211',
  //   component: 'ApiSelect',
  //   componentProps: {
  //     api: getRoleSelectList,
  //     mode: 'multiple',
  //   },
  // },

  {
    field: 'tel',
    label: '电话号码',
    component: 'Input',
  },
  // TODO: 上传组件
  // {
  //   field: 'avatar',
  //   label: '头像',
  //   component: 'Upload',
  // },
  // {
  //   field: 'gender',
  //   label: '性别',
  //   component: 'RadioButtonGroup',
  //   defaultValue: 0,
  //   componentProps: {
  //     options: [
  //       { label: '未知', value: 0 },
  //       { label: '男', value: 1 },
  //       { label: '女', value: 2 },
  //     ],
  //   },
  // },
  {
    field: 'email',
    label: '邮箱',
    component: 'Input',
    rules: [
      {
        validator: (_, value) => {
          const mail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
          if (mail.test(value)) {
            return Promise.resolve();
          } else {
            return Promise.reject('邮箱格式不正确!');
          }
        },
      },
    ],
  },
  // {
  //   field: 'email_status',
  //   label: '邮箱状态',
  //   component: 'RadioButtonGroup',
  //   defaultValue: 0,
  //   componentProps: {
  //     options: [
  //       { label: '未验证', value: 0 },
  //       { label: '已验证', value: 1 },
  //     ],
  //   },
  // },

  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    colProps: {
      lg: 32,
      md: 24,
    },
    componentProps: {
      rows: 4,
    },
  },
];
